
package com.karthik.action;

import com.karthik.entity.Employee;
import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;


@Name("employeeaction")
public class EmployeeSaveAction implements Serializable{

    @In
    EntityManager em;
    private Employee employee;
    @DataModel
    List<Employee> employeeList;
    @DataModelSelection("employeeList")
    Employee selectedEmployee;
 

    @Create
    public void init() {
        employee = new Employee();
    }

    @Factory("employeeList")
    public void populateEmployeeList(){
        employeeList=em.createNamedQuery("Employee.findAll").getResultList();
    }

    public void createEmployee() {
        em.persist(employee);
        //clear UI values
        employee = new Employee();
        populateEmployeeList();
    }

    public void update(){
        selectedEmployee=em.merge(selectedEmployee);
        populateEmployeeList();
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}
